<?php if (!defined('SYSTEM_ROOT')) {
    die('Insufficient Permissions');
}
if (ROLE !== 'admin') {
    msg('权限不足!');
    die;
}
switch ($_GET['act']) {
    case 'ok'://成功回显
        echo '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>插件设置成功</div>';
        break;
    case 'debug': //测试推送发送回显
        ob_end_clean();
        if ($_POST['is_send'] != 'ok' || empty($_POST['sckey']) || empty($_POST['title'])) {
            $return_arr = array('code' => 0, 'msg' => '无效请求!');
        } else {
            global $i;
            $user_name = $i['user']['name'];
            require 'class.serverchan_utils.php';
            $serverchan_utils_obj = new serverchan_utils();
            $title = $serverchan_utils_obj->deal_user_template($_POST['title'], $user_name);
            $content = $serverchan_utils_obj->deal_user_template($_POST['content'], $user_name);
            $return_arr = $serverchan_utils_obj->push_notice($_POST['sckey'], $title, $content);
        }
        if ($return_arr['code'] == 0) {
            $msg_html = '<div class="text-success"><span class="glyphicon glyphicon-ok-sign"></span>';
        } else {
            $msg_html = '<div class="text-danger"><span class="glyphicon glyphicon-remove-sign"></span>';
        }
        $msg_html .= '&nbsp;' . $return_arr['msg'] . '</div>';
        echo json_encode(array('code' => $return_arr['code'], 'msg' => $msg_html));
        die;
        break;
    case 'store'://保存设置
        option::set('tieba_serverchan_default_open', $_POST['tieba_serverchan_default_open']);
        option::set('tieba_serverchan_send_hour', intval($_POST['tieba_serverchan_send_hour']));
        option::set('tieba_serverchan_title', htmlspecialchars_decode($_POST['tieba_serverchan_title']));
        option::set('tieba_serverchan_content', htmlspecialchars_decode($_POST['tieba_serverchan_content']));
        ReDirect(SYSTEM_URL . 'index.php?mod=admin:setplug&plug=tieba_serverchan&act=ok');
        die;
    default:
        break;
}


?>
<form action="index.php?mod=admin:setplug&plug=tieba_serverchan&act=store" method="post">
    <div class="container">
        <h3 class="align:center;">自定义签到微信推送设置</h3>
    </div>

    <table class="table table-condensed table-hover">
        <thead>
        <tr>
            <th class="col-md-2">功能模块</th>
            <th>设置模块</th>
        </tr>
        </thead>
        <tr>
            <td>插件设置</td>
            <td>
                <div class="input-group">
                    <input type="checkbox"
                           name="tieba_serverchan_default_open"
                           value="1" <?php echo option::get('tieba_serverchan_default_open') ? 'checked' : ''; ?>>
                    所有用户默认开启微信推送
                </div>
                <br>
                <div class="input-group">
                    <span class="input-group-addon">每天</span>
                    <input class="form-control" type="number" placeholder="0点~23点,超过范围无效"
                           name="tieba_serverchan_send_hour"
                           value="<?php echo option::get('tieba_serverchan_send_hour') ?>">
                    <span class="input-group-addon">点推送</span>
                </div>
                <br>
            </td>
        </tr>
        <tr>
            <td>模板变量</td>
            <td>
                <div><ul>
                    <li>[date]当前日期</li>
                    <li>[name]用户名</li>
                    <li>[details]签到详情</li>
                    <li>[SYSTEM_URL]站点url</li>
                    <li>[SYSTEM_NAME]站点名称</li>
                </ul></div>
            </td>
        </tr>
        <tr>
            <td>模板设置</td>
            <td>
                <div class="input-group">
                    <span class="input-group-addon">消息标题</span>
                    <input class="form-control" type="text"
                           id="title_input"
                           name="tieba_serverchan_title"
                           value="<?php echo htmlspecialchars(option::get('tieba_serverchan_title')); ?>">
                </div>
                <br>
                <div class="input-group">
                    <span class="input-group-addon">消息正文</span>
                        <textarea class="form-control"
                                  id="content_input"
                                  name="tieba_serverchan_content"
                                  style="height:150px;"><?php echo htmlspecialchars(option::get('tieba_serverchan_content')) ?></textarea>
                </div>
                <br>
            </td>
        </tr>
        <tr>
            <td>调试测试</td>
            <td>
                <br>
                <div class="input-group">
                    <span class="input-group-addon">测试SCKEY</span>
                    <input id="sckey_input" class="form-control" type="text" value="<?php echo htmlspecialchars(option::uget('tieba_serverchan_sckey')) ?>">
                </div>
                <br>
                <a id="debug_btn" href="javascript:void(0)" class="btn btn-warning">
                    点击测试
                </a>
                <span id="debug_tip"></span>
                <br>
            </td>
        </tr>
    </table>

    <button type="submit" class="btn btn-info">保存设置</button>
</form>
<script type="text/javascript">
    var click_status = 0;
    $(document).delegate('#debug_btn', 'click', function () {
        if (click_status > 0)return;
        click_status = 1;
        $('#debug_btn').attr('disabled', true);
        $('#debug_btn').text('正在发送');
        $.ajax({
            url: 'index.php?mod=admin:setplug&plug=tieba_serverchan&act=debug',
            type: 'POST',
            dataType: 'json',
            data: {
                is_send: 'ok',
                sckey: $('#sckey_input').val(),
                title: $('#title_input').val(),
                content: $('#content_input').val()
            },
            success: function (result) {
                switch (result.code) {
                    case 0:
                    case 1:
                        alert(result.msg);
                        break;
                    default:
                        alert('请求异常!请刷新页面后重试');
                        break;
                }
                $('#debug_btn').attr('disabled', false);
                $('#debug_btn').text('点击测试');
                click_status = 0;
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('网络异常!请刷新页面后重试: ' + errorThrown);
                $('#debug_btn').attr('disabled', false);
                $('#debug_btn').text('点击测试');
                click_status = 0;
            }
        });
    });
</script>